package com.google.zxing.client.ohos.camera;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.InnerEvent;
import ohos.media.image.Image;
import ohos.media.image.ImageReceiver;
import ohos.media.image.common.ImageFormat;
import ohos.utils.PacMap;

//单帧捕获生成图像回调Listener
public class ImageReceiverCallback implements ImageReceiver.IImageArrivalListener {
    private EventHandler eventHandler;

    public ImageReceiverCallback() {

    }

    public void setHandler(EventHandler eventHandler){
        this.eventHandler = eventHandler;
    }
    @Override
    public void onImageArrival(ImageReceiver imageReceiver) {
        Image myImage = imageReceiver.readNextImage();
        Image.Component component = myImage.getComponent(ImageFormat.ComponentType.JPEG);
        byte[] bytes = new byte[component.remaining()];
        component.read(bytes);

        if (eventHandler != null){
            InnerEvent innerEvent = InnerEvent.get(DecodeHandler.RECIVEIMAGE);
            PacMap pacMap = new PacMap();
            pacMap.putIntValue("width",myImage.getImageSize().width);
            pacMap.putIntValue("height",myImage.getImageSize().height);
            innerEvent.object = bytes;
            eventHandler.sendEvent(innerEvent);
            eventHandler = null;
        }
    }
}
